<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>帖子留言</title>
    <script src="../node_modules/jquery/dist/jquery.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            margin: 0;
            padding: 20px;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .post-header {
            padding-bottom: 15px;
            margin-bottom: 20px;
            border-bottom: 1px solid #eee;
        }
        .post-title {
            font-size: 24px;
            margin: 0 0 10px 0;
            color: #333;
        }
        .post-meta {
            color: #777;
            font-size: 14px;
        }
        .post-content {
            margin: 20px 0;
            line-height: 1.6;
        }
        .comments-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin: 30px 0 15px 0;
        }
        .comments-title {
            font-size: 20px;
            margin: 0;
        }
        .add-comment-btn {
            background-color: #e20117;
            color: white;
            border: none;
            padding: 8px 15px;
            border-radius: 4px;
            cursor: pointer;
        }
        .comment-list {
            margin-top: 20px;
        }
        .comment-item {
            padding: 15px;
            margin-bottom: 15px;
            background: #f9f9f9;
            border-radius: 5px;
            position: relative;
        }
        .comment-content {
            margin: 10px 0;
        }
        .comment-meta {
            display: flex;
            justify-content: space-between;
            color: #777;
            font-size: 13px;
        }
        .comment-actions {
            position: absolute;
            top: 15px;
            right: 15px;
        }
        .delete-btn {
            background-color: #ff4d4f;
            color: white;
            border: none;
            padding: 3px 8px;
            border-radius: 3px;
            cursor: pointer;
            font-size: 12px;
        }
        .comment-form {
            margin-top: 20px;
            padding: 15px;
            background: #f0f0f0;
            border-radius: 5px;
            display: none;
        }
        .comment-form textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            min-height: 80px;
            margin-bottom: 10px;
        }
        .form-actions {
            text-align: right;
        }
        .submit-btn {
            background-color: #e20117;
            color: white;
            border: none;
            padding: 8px 15px;
            border-radius: 4px;
            cursor: pointer;
        }
        .cancel-btn {
            background-color: #ddd;
            color: #333;
            border: none;
            padding: 8px 15px;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 10px;
        }
        .back-link {
            display: inline-block;
            margin-bottom: 20px;
            color: #e20117;
            text-decoration: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <button onclick="fan()">返回相册</button>
        <div class="comments-header">
            <h1 class="comments-title">贴吧</h1>
            <button class="add-comment-btn" id="showCommentForm">添加留言</button>
        </div>
        
        <div class="comment-form" id="commentForm">
            <textarea id="commentContent" placeholder="请输入留言内容..."></textarea>
            <div class="form-actions">
                <button class="cancel-btn" id="cancelComment">取消</button>
                <button class="submit-btn" id="submitComment" onclick="submitComment()">提交</button>
            </div>
        </div>
        <div class="comment-list" id="commentList">
        </div>
    </div>

    <script>
        // 页面加载时初始化
        $(document).ready(function() {
            // 显示/隐藏留言表单
            $('#showCommentForm').click(function() {
                $('#commentForm').show();
            });
            
            $('#cancelComment').click(function() {
                $('#commentForm').hide();
                $('#commentContent').val('')
            });
            
            // 加载帖子详情和留言
            loadPostDetail();
            loadComments();
        });
        
        // 加载帖子详情（空方法，待实现）
        function loadPostDetail() {
            // 使用AJAX请求获取帖子详情
            // 示例结构：
            /*
            $.ajax({
                url: "api/post/detail",
                type: "POST",
                data: { post_id: postId },
                success: function(data) {
                    // 更新帖子标题、作者、内容和时间
                }
            });
            */
        }
        
        // 加载留言列表（空方法，待实现）
        function loadComments() {
            var textid=localStorage.getItem("id");
            // 使用AJAX请求获取留言列表
            // 示例结构：
             $.ajax({
                url: "http://localhost:3001/api/all",
                type: "POST",
                data: { textid: textid },
                success: function(data) {
                    // 渲染留言列表
                    var html = '';
                    data.data.forEach(function(item) {
              html += `
                <div class="comment-item">
                <div class="comment-content">
                ${item.nei}
                </div>
                <div class="comment-meta">
                    <!-- <span class="comment-author">留言用户</span> -->
                    <span class="comment-time">${item.time}</span>
                </div>
                <div class="comment-actions">
                    <button class="delete-btn" onclick="deleteComment(${item.id})">删除</button>
                </div>
            </div>
              `;
            });
            $("#commentList").html(html);
                    // renderComments(data.comments);
                }
            });
        }loadComments()
        
        // 渲染留言列表（空方法，待实现）
        function renderComments(comments) {
            // 根据comments数组渲染留言列表
            // 每行一条留言，包含内容、创建时间和删除按钮
        }
        
        // 提交留言（空方法，待实现）
        function submitComment() {
            var textid=localStorage.getItem("id");
            console.log(textid);
            
            // console.log(111);
            
            // 处理留言提交
           $.ajax({
                url: "http://localhost:3001/api/add",
                type: "POST",
                data: { 
                    textid: textid,
                    nei: $('#commentContent').val() 
                },
                success: function(data) {
                    if (data.code == 200){
              alert("上传成功");
              $('#commentContent').val('')
            loadComments()
            }else{
              alert("上传失败");
            }
                }

            });
        }
        
        // 删除留言（空方法，待实现）
        function deleteComment(id) {
            // 处理留言删除
            var iid=id
            
            $.ajax({
                url: "http://localhost:3001/api/del",
                type: "POST",
                data: { id: iid },
                success: function(data) {
                    if (data.code == 200){
              alert("删除成功");
            loadComments()
            }else{
              alert("删除失败");
            }
                    // 处理删除成功后的逻辑
                }
            });
            
        }

        function fan(){
            window.location.href = './album.html1   1';
        }
    </script>
</body>
</html>